package com.szholly.pro.watergas_server.common.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.szholly.utils.cache.ehcache.PageCacheFilter;
import com.szholly.utils.exception.ServiceException;

public class WebSessionFilter implements Filter{
	private Logger logger = Logger.getLogger(PageCacheFilter.class);

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	String sessionId="";
	String userAgent = "";
	String SecondSessionId="";
	String SecondUserAgent ="";
	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {

/*		// 从 HTTP 头中取得 Referer 值
		if("".equals(sessionId)&&"".equals(userAgent)) {
			sessionId=((HttpServletRequest) arg0).getSession().getId();
			userAgent = ((HttpServletRequest) arg0).getHeader("User-Agent");
			arg2.doFilter(arg0,arg1);
		} else {
			SecondSessionId=((HttpServletRequest) arg0).getSession().getId();
			SecondUserAgent = ((HttpServletRequest) arg0).getHeader("User-Agent");
			if ("".equals(SecondUserAgent)||(sessionId.equals(SecondSessionId)&&userAgent.equals(SecondUserAgent))) {
				logger.info("您的合法 ");
				arg2.doFilter(arg0,arg1);
			} else {
			    try {
					throw new ServiceException("您的违规操作将被中止、记录并保留追溯权利！");
				} catch (ServiceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}*/
		String referer=((HttpServletRequest) arg0).getHeader("Referer");  

	     if((referer==null) ||(referer.trim().contains("WaterMonitor"))){  

	    	 arg2.doFilter(arg0, arg1);  

	     } else{  
	    	 arg0.getRequestDispatcher("error.jsp").forward(arg0,arg1);  

	     } 
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
